package com.heima.admin.controller;

import com.heima.admin.entity.AdChannel;
import com.heima.admin.service.AdChannelService;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.model.admin.dtos.ChannelDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/v1/channel")
@Api(tags = "频道管理",description = "频道管理api")
public class ChannelController {

    @Autowired
    private AdChannelService channelService;

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据分页查询所有频道")
    @ApiImplicitParam(name = "dto",value = "频道请求对象",required = true,dataType = "ChannelDto")
    @PostMapping("/list")
    public PageResult<ChannelDto> findByPage(@RequestBody ChannelDto dto){
        return channelService.findByPage(dto);
    }

    /**
     * 新增频道
     */
    @PostMapping("/save")
    public ResponseResult save(@RequestBody ChannelDto channelDto){
        //判断接收参数，转换为AdChannel
        if (channelDto == null || StringUtils.isBlank(channelDto.getName())){
            //为空则返回参数错误
            return ResponseResult.error(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        AdChannel adChannel = BeanHelper.copyProperties(channelDto, AdChannel.class);
        //调用service
        adChannel.setCreatedTime(new Date());
        boolean b = channelService.save(adChannel);
        //判断是否插入成功
        if (!b){
            //不成功则抛出异常
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok();
    }
    /**
     * 更新频道
     */
    @PutMapping("/update")
    public ResponseResult update(@RequestBody ChannelDto channelDto){
        //接收并判断参数
        if (channelDto==null || channelDto.getId()==null){
            //返回错误提示
            return ResponseResult.error(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //转换对象
        AdChannel adChannel = BeanHelper.copyProperties(channelDto, AdChannel.class);
        //调用service
        boolean b = channelService.updateById(adChannel);
        //判断是否成功
        if (!b){
            //失败给出错误提示
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok();
    }
    /**
     * 删除频道
     */
    @DeleteMapping("/del/{id}")
    public ResponseResult deleteById(@PathVariable(value = "id") Integer channelId){
        //1.判断该id下实体的状态是否为true和实体是否存在
        if (channelService.getById(channelId) == null){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (channelService.getById(channelId).getStatus()){
            //为true则不能删除
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID,"只有在无效状态才能删除");
        }
        //2.调用service
        boolean b = channelService.removeById(channelId);
        //3.判断是否删除成功
        if (!b){
            //删除失败
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR,"删除失败");
        }
        return ResponseResult.ok();
    }

    /**
     * 查询所有频道
     * @return
     */
    @GetMapping("/channels")
    public ResponseResult<AdChannel> findAllAdChannels(){
        List<AdChannel> list = channelService.list();
        //判断
        if (CollectionUtils.isEmpty(list)){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.ok(list);
    }
}